package org.aom.litebean.module.security;

import org.aom.entity.security.Role;
import org.aom.litebean.module.base.BaseGridCrudBean;
import org.aom.service.security.IRoleManager;
import org.aom.util.ManageBeanUtil;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.component.grid.provider.GridDataProvider;

@ManagedBean(name = "module.security.roleListBean", scope = ManagedBeanScope.REQUEST)
public class RoleListBean extends BaseGridCrudBean<Role, Long> {

	@ManagedProperty("#{roleManager}")
	private IRoleManager roleManager;

	@Bind(id = "roleDataGrid", attribute = "idColumn")
	private static String idColumn = "id";

	@Bind(id = "roleDataGrid", attribute = "binding")
	private UIDataGrid roleDataGrid;

	@Override
	public IRoleManager getEntityManager() {
		return this.roleManager;
	}

	@Override
	public Class<Role> getEntityClass() {
		return Role.class;
	}

	@Override
	protected String getIdColumn() {
		return idColumn;
	}

	@Bind(id = "roleDataGrid", attribute = "dataProvider")
	public GridDataProvider getDataProvider() {
		return super.getDataProvider();
	}

	@BeforeRender
	public void beforeRender(boolean isPostBack) {
		initBaseResource();
	}

	@Override
	@Action(id = "add", event = "onclick")
	protected void processToAdd() {
		String url = "roleForm.faces";
		ManageBeanUtil.openDialog(null, "add", OperationFrame, url,
				OperationDialog, 500, 150);
	}

	@Override
	@Action(id = "roleDataGrid", event = "ondblclick")
	protected void processToEdit() {
		try {
			Role role = (Role) getSelectEntity();
			String url = "roleForm.faces";
			ManageBeanUtil.openDialog(role.getId(), "mod", OperationFrame, url,
					OperationDialog, 500, 150);
		} catch (ArrayIndexOutOfBoundsException e) {
			ManageBeanUtil.noSelectWarn();
		}
	}

	@Action(id = "linkAuths")
	public void linkAuths() {
		try {
			Role role = (Role) getSelectEntity();
			String url = "roleLinkAuthorites.faces";
			ManageBeanUtil.openDialog(role.getId(), null, OperationFrame, url,
					OperationDialog, 800, 400);
		} catch (ArrayIndexOutOfBoundsException e) {
			ManageBeanUtil.noSelectWarn();
		}
	}

	@Override
	protected void processToSearch() {

	}

	@Override
	protected void initAdd() {

	}

	@Override
	protected UIDataGrid getDataGrid() {
		return this.roleDataGrid;
	}

	@Override
	protected void delete() {
		try {
			Role entity = (Role) getSelectEntity();
			roleManager.delete(entity.getId());
			resetGrid();
		} catch (ArrayIndexOutOfBoundsException e) {
			ManageBeanUtil.noSelectWarn();
		}
	}

}
